package com.yxc.mamba;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.squareup.okhttp.*;
import com.squareup.okhttp.Response;
import com.yxc.mambalibrary.http.*;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    public final String TAG = "MainActivity";

    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        text = (TextView) findViewById(R.id.text);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
//                testHttp();
                testLib();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void testHttp(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url("http://www.baidu.com").build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.d(TAG, e.toString());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.d(TAG, response.toString());
                text.setText("HAHA");
            }
        });
    }

    private void testLib(){
        IRequestParameter parameter = new DefaultParameter();
        parameter.setUrl("http://apis.baidu.com/txapi/tiyu/tiyu");
        parameter.addParameter("num", "10");
        parameter.addParameter("page", "1");
        parameter.addHeader("apikey", "ac9c1c27e1997fc2e8e4e6a27a70aae8");
        RequestManager.get("API Store", HttpType.OKHTTP, parameter, new DefaultCallBack() {
            @Override
            public void onFailure(IRequest request, RequestException exception) {
                text.setText(exception.getMessage());
            }

            @Override
            public void onSuccess(IRequest request, IResponse response) {
                text.setText(response.getResult().toString());
            }
        });
    }
}
